cphsoc_stata_export <- function(df,
                                stata_do_path,
                                infile_data_path,
                                stata_csv_path,
                                renamed_vars_path,
                                na='',
                                target_enc='',
                                stata_dta_path,
                                dta_replace=FALSE)
{
  output_report <- list()
  
  var_names <- names(df)
  var_types  <- sapply(df,'typeof')
  missings   <- attr(df,'missings')
  val_labels <- attr(df,'value.labels')
  var_labels <- attr(df,'variable.labels')
  # remove linefeeds from variable labels
  var_labels <- gsub('\\r|\\n','',var_labels)
  
  ####################################################
  # PREPARE FOR STATA
  ####################################################
  
  ####################################################
  # prepare string variables
  # replace double quotes with single quotes
  # generate type command for stata (only for string)
  ####################################################
  df[] <- lapply(df,function(z){if(is.character(z)) gsub('"',"'",z,fixed=TRUE) else z})
  
  # max length of each character variable
  char_var_max_lengths <- lapply(df,function(z) if(is.character(z)) max(nchar(z)) else "")

  ####################################################
  # save raw data for stata import
  ####################################################
  con <- file(stata_csv_path,"w+",encoding=target_enc)
  write.table(df,file=con,na=na,sep=',',qmethod='escape',col.names=FALSE,row.names=FALSE)
  close(con)
  output_report[[length(output_report)+1]] <- paste("Data for Stata import saved as: ",stata_csv_path,sep='')
  
  df <- NULL

  ####################################################
  ## make variable names legal for stata
  ####################################################
  vnm_new <- cphsoc_stata_legal_varnames(var_names,len=28,ptrn='[#\\.\\$@]',index.return=TRUE)
  
  # save list of changed variable names
  if(length(vnm_new$ix)>0)
  {
    stata_renamed_vars <- paste('"',var_names[vnm_new$ix],'" renamed to "',vnm_new$x[vnm_new$ix],'"',sep='')
    # write to file
    if(length(stata_renamed_vars)>0) 
    {
      stata_renamed_vars <- paste(stata_renamed_vars,collapse='\n')
      con <- file(renamed_vars_path,"w+",encoding=target_enc)
      cat(file=con,stata_renamed_vars)
      close(con)
      output_report[[length(output_report)+1]] <- paste("Some variable names were changed, please see: ",renamed_vars_path,sep='')
      rm(stata_renamed_vars)
    }
  }
  var_names <- vnm_new$x
  
  ####################################################
  # create variable definitions for input do file
  ####################################################
  stata_do_str <- cphsoc_stata_generate_dofile( var_labels=var_labels,
                                                var_types=var_types,
                                                data_path=infile_data_path,
                                                var_names=var_names,
                                                val_labels=val_labels,
                                                missings=missings,
                                                dta_replace=dta_replace,
                                                stata_dta_path=stata_dta_path,
                                                char_var_max_lengths=char_var_max_lengths)
  
  if(nchar(stata_do_str)>0)
  {
    con <- file(stata_do_path,"w+",encoding=target_enc)
    res <- try(cat(file=con,stata_do_str))
    close(con)
    output_report[[length(output_report)+1]] <- paste("Stata do-file saved as: ",stata_do_path,sep='')
    rm(stata_do_str)
  }
  if(class(res)=="try-error")
  {
    warning(res)
    return(res)
  }
  else
  {
    return(output_report)
  }
}